package cn.stimd.spring.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Stimd
 * 项目地址：https://gitee.com/stimd/spring-wheel
 * 公众号：Java编程探微
 */
public class MutablePropertyValues implements PropertyValues{
    private final List<PropertyValue> propertyValueList;

    public MutablePropertyValues() {
        this.propertyValueList = new ArrayList<>(0);
    }

    public MutablePropertyValues(PropertyValues original) {
        PropertyValue[] pvs = original.getPropertyValues();
        this.propertyValueList = new ArrayList<>(pvs.length);
        for (PropertyValue pv : pvs) {
            this.propertyValueList.add(new PropertyValue(pv));
        }
    }


    public MutablePropertyValues(Map<?, ?> original) {
        this.propertyValueList = new ArrayList<>(original.size());
        for (Map.Entry<?, ?> entry : original.entrySet()) {
            this.propertyValueList.add(new PropertyValue(entry.getKey().toString(), entry.getValue()));
        }
    }

    public void addPropertyValue(String propertyName, Object propertyValue) {
        this.propertyValueList.add(new PropertyValue(propertyName, propertyValue));
    }


    @Override
    public PropertyValue[] getPropertyValues() {
        return this.propertyValueList.toArray(new PropertyValue[0]);
    }

    @Override
    public PropertyValue getPropertyValue(String propertyName) {
        for (PropertyValue pv : this.propertyValueList) {
            if (pv.getName().equals(propertyName)) {
                return pv;
            }
        }
        return null;
    }
}
